<template>
  <div class="wrap">
    <div class="login">
      <div class="logo">
        <img src="../images/login/logo.png" alt>
      </div>
      <div class="title">亲子产业共创管理中心</div>
      <div class="input first">
        <input type="text" placeholder="请输入帐号" v-model="userInfo.name">
      </div>
      <div class="input">
        <input type="password" placeholder="请输入密码" v-model="userInfo.pwd" @input="setPwd">
      </div>
      <div class="input">
        <input type="number" class="code" placeholder="请输入验证码" v-model="code" @input="setCode">
        <div class="num" @click="getCode" style="cursor:pointer">{{recode}}</div>
      </div>
      <button class="input btn" @click="login()">登 录</button>
      <div class="info">忘记密码请联系系统管理员进行重置</div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
export default {
  name: "login",
  data() {
    return {
      userInfo: {
        name: "",
        pwd: ""
      },
      code: "",
      recode: "",
      canLogin: true,
      isEnter: true
    };
  },
  methods: {
    /**
     * 设置密码
     */
    setPwd: function() {
      this.userInfo.pwd = this.userInfo.pwd.replace(/[^\w\.\/]/gi, "");
    },
    /*
     * 设置验证码
     */
    setCode: function() {
      if (this.code.length > 6) {
        this.code = this.code.substring(0, 6);
      }
    },
    /**
     * 登录
     */
    login: function() {
      let that = this;
      if (that.userInfo.name == "") {
        that.isEnter = false;
        that.$alert("账号不能为空!", {
          confirmButtonText: "确定",
          callback: function() {
            that.isEnter = true;
          }
        });
        return;
      }
      // console.log(that.userInfo.pwd.length)
      if (that.userInfo.pwd.length == 0) {
        that.isEnter = false;
        that.$alert("密码不能为空!", {
          confirmButtonText: "确定",
          callback: function() {
            that.isEnter = true;
          }
        });
        return;
      }
      if (that.recode == that.code) {
        if (!that.canLogin) {
          return;
        }
        that.canLogin = false;
        axios({
          method: "post",
          url: `/service/login`,
          data: qs.stringify(that.userInfo)
        }).then(res => {
          that.canLogin = true;
          if (res.data.code == 0) {
            document.onkeydown = function () {
            if (key == 13) {
            }
          }
            this.$store.commit("servicePwd", res.data.data.servicePwd);
            this.$store.commit("userId", res.data.data.id);
            for (let key in res.data.data) {
              if (key == "id") {
                this.$cookie.set("serviceId", res.data.data[key]);
              } else {
                this.$cookie.set(key, res.data.data[key]);
              }
            }
            that.$router.push({
              path: "/main"
            });
          } else {
            that.isEnter = false;
            that.$alert(res.data.msg, {
              confirmButtonText: "确定",
              callback: function() {
                that.isEnter = true;
                this.getCode();
              }
            });
          }
        });
      } else {
        that.isEnter = false;
        this.$alert(this.code != "" ? "验证码错误!" : "请输入验证码", {
          confirmButtonText: "确定",
          callback: function() {
            that.isEnter = true;
            that.getCode()
          }
        });
      }
    },
    /**
     * 获取验证码 随机生成6位的验证码
     */
    getCode: function() {
      this.recode = "";
      for (var i = 0; i < 6; i++) {
        this.recode += Math.floor(Math.random() * 10);
      }
    }
  },
  mounted() {
    this.getCode();
  },
  created() {
    var that = this;
    document.onkeydown = function(e) {
      var key = window.event.keyCode;
      if (key == 13) {
        if (that.isEnter) {
          that.login();
         
        }
      }
    };
  }
};
</script>

<style scoped>
.wrap {
  width: 100%;
  height: 100vh;
  background: url("../images/login/bg.png");
}
.login {
  padding: 9px 27px;
  background: #ffffff;
  box-shadow: 0 2px 7px 0 rgba(0, 0, 0, 0.5);
  border-radius: 40px;
  width: 700px;
  height: 562px;
  box-sizing: border-box;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}
.logo {
  width: 140px;
  height: 54px;
}
.title {
  height: 50px;
  line-height: 50px;
  font-family: PingFangSC-Medium;
  font-size: 36px;
  color: #000;
  text-align: center;
  margin-top: 5px;
}
.input {
  box-shadow: 0 2px 4px 0 rgba(143, 143, 143, 0.5);
  border-radius: 100px;
  width: 338px;
  height: 49px;
  border: 1px solid #0e64e1;
  border-radius: 25px;
  margin: 0 auto;
  margin-top: 19px;
  color: #9b9b9b;
}
.first {
  margin-top: 50px;
}
.num {
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: #333333;
  float: right;
  border-left: 1px solid #0e64e1;
  width: 80px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  letter-spacing: 1.54px;
}
.code {
  width: 250px;
  float: left;
}
.btn {
  margin: 0 auto;
  margin-top: 45px;
  background: #0e64e1;
  color: #fff;
  box-shadow: 0 0 8px #bcbcbc;
  outline: none;
  cursor: pointer;
  font-family: PingFangSC-Medium;
  display: block;
}
.info {
  margin-top: 34px;
  line-height: 20px;
  text-align: center;
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: #9b9b9b;
  height: 20px;
}
input {
  width: 100%;
  border: 0;
  outline: none;
  height: 100%;
  background: none;
  text-indent: 2em;
  border: 0px;
  box-shadow: 0 0 0px 1000px transparent inset !important;
  -webkit-box-shadow: 0 0 0px 1000px transparent inset !important;
  background-color: transparent;
  background-image: none;
  transition: background-color 50000s ease-in-out 0s;
}
</style>


